﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;

using GDIBitmap = System.Drawing.Bitmap;
using GDIGraphicsUnit = System.Drawing.GraphicsUnit;
using GDIRectangle = System.Drawing.Rectangle;

namespace SharpGameLibrary
{
    public class Tileset
    {
        #region Fields

        Bitmap image;
        String name;
        Tile[,] tiles;
        bool isInitialized;
        int maxX;

        #endregion

        #region Get / Set

        public Bitmap Image
        {
            get
            {
                return this.image;
            }
        }

        public bool IsInitialized
        {
            get
            {
                return this.isInitialized;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
        }

        public int MaxX
        {
            get
            {
                return this.maxX;
            }
        }

        #endregion

        #region Constructor

        public Tileset(Bitmap image, String name)
        {
            this.image = image;
            this.name = name;
            this.isInitialized = false;
            this.maxX = image.Width / TileEngine.Tile.Width;
        }

        #endregion

        #region Methods

        public Tile getTile(int tileX, int tileY)
        {
            return this.tiles[tileX, tileY];
        }

        public void initialize(Size tile)
        {
            if (!this.isInitialized)
            {
                int nbTilesWidth = this.image.Width / tile.Width;
                int nbTilesHeight = this.image.Height / tile.Height;
                int nbTiles = nbTilesWidth * nbTilesHeight;

                this.tiles = new Tile[nbTilesWidth, nbTilesHeight];

                Bitmap tileImg;
                Graphics g;
                Rectangle dest;
                Rectangle soure;

                int x = 0;
                int y = 0;

                for (int curTile = 0; curTile < nbTiles; curTile++)
                {
                    dest = new Rectangle(0, 0, tile.Width, tile.Height);
                    soure = new Rectangle(x * tile.Width, y, tile.Width, tile.Height);

                    tileImg = new Bitmap(tile.Width, tile.Height);

                    g = Graphics.FromImage(tileImg);
                    g.DrawImage(this.image, dest, soure, GDIGraphicsUnit.Pixel);
                    g.Dispose();

                    this.tiles[x, y / tile.Height] = new Tile(tileImg, x + y);

                    x++;

                    if (x == nbTilesWidth)
                    {
                        y += tile.Height;
                        x = 0;
                    }
                }
            }

            this.isInitialized = true;
        }

        #endregion

    }
}
